﻿using StageToDo.Entity.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace StageToDo.Service.Service
{
    public class UserService
    {
        private StageToDoContext data;
        public UserService()
        {
            data = new StageToDoContext();
        }
        public User Login(string email, string password)
        {
            var user = data.User.SingleOrDefault(u => u.Email == email && u.Password == password);
            return user;
        }

        public User GetUserInfo(string userEmail)
        {
            return data.User.SingleOrDefault(u => u.Email == userEmail);
        }

        public User UserRegister(string email, string password, string nickname)
        {
            User user = new User
            {
                Email = email,
                Nickname = nickname,
                Password = password,
                CreateTime = DateTime.Now
            };
            data.User.Add(user);
            data.SaveChanges();
            return user;
        }

        public bool ModifyPassword(string userEmail, string oldPassword, string newPassword)
        {
            var user = data.User.SingleOrDefault(u => u.Email == u.Email& u.Password == oldPassword);
              if(user != null)
            {
                user.Password = newPassword;
                data.SaveChanges();
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool ModifyPersonalInformation(string userEmail, string nickname)
        {
            throw new NotImplementedException();
        }
    }
}
